/*
Write a network server that stores name-value pairs. The server should allow names
to be added, deleted, modified, and retrieved by clients. Write one or more client
programs to test the server. Optionally, implement some kind of security
mechanism that allows only the client that created the name to delete it or to
modify the value associated with it.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

static int
inet_connect(const char *host, const char *service, int type)
{
  struct addrinfo hint;
  memset(&hint, 0, sizeof hint);
  hint.ai_family = AF_UNSPEC;
  hint.ai_socktype = type;

  struct addrinfo *result, *rp;
  int ret = getaddrinfo(host, service, &hint, &result);
  if (ret != 0)
    {
      errno = ENOSYS;
      return -1;
    }

  int sfd;
  for (rp = result; rp != NULL; rp = rp->ai_next)
    {
      sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
      if (sfd == -1) continue;

      if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) break;

      close(sfd);
    }

  freeaddrinfo(result);

  return (rp == NULL) ? -1 : sfd;
}

static int
inet_passive_socket(const char *service, int type, socklen_t *addrlen,
                    bool listen, int backlog)
{
  struct addrinfo hint;
  memset(&hint, 0, sizeof hint);
  hint.ai_family = AF_UNSPEC;
  hint.ai_socktype = type;
  hint.ai_flags = AI_PASSIVE;

  struct addrinfo *result, *rp;
  int ret = getaddrinfo(NULL, service, &hint, &result);
  if (ret != 0)
    return -1;

  
}

static int
inet_listen(const char *service, int backlog, socklen_t *addrlen)
{
}

static int
inet_bind(const char *service, int type, socklen_t *addrlen)
{
}

static char *
inet_address_str(const struct sockaddr *addr, socklen_t addrlen,
                 char *addrstr, int addrstrlen)
{
}

int
main(void)
{
  return EXIT_SUCCESS;
}
